package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.ShelvesgroupService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.service.GoodshelvesService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-03-20 09:47:30
 */
@RestController
@RequestMapping("sys/goodshelves")
public class GoodshelvesController {
    @Autowired
    private GoodshelvesService goodshelvesService;

    @Autowired
    private ShelvesgroupService shelvesgroupService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:goodshelves:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodshelvesService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 条件查询货架
     */
    @RequestMapping("/listByParams")
    @RequiresPermissions("sys:goodshelves:listByParams")
    public R listByParams(@RequestParam Map<String, Object> params){
        PageUtils page = goodshelvesService.querylistByParams(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/getAnalysisList")
    @RequiresPermissions("sys:goodshelves:getAnalysisList")
    public R getAnalysisList(@RequestParam Map<String, Object> params){
        List<ShelvesAnalysisEntity> list = goodshelvesService.getShelvesAnalysis(params);

        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:goodshelves:info")
    public R info(@PathVariable("id") Integer id){
        GoodshelvesEntity goodshelves = goodshelvesService.getById(id);

        return R.ok().put("goodshelves", goodshelves);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:goodshelves:save")
    public R save(@RequestBody GoodshelvesEntity goodshelves){
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        goodshelves.setCreatetime(new Date());
        goodshelves.setShopid(Integer.parseInt(user.getUserId().toString()));
        if(StringUtils.isNotBlank(goodshelves.getShelvesgroup())){
            String[] groups = goodshelves.getShelvesgroup().split(",");
            for(String id:groups){
                ShelvesgroupEntity shelvesgroupEntity = shelvesgroupService.getById(Integer.parseInt(id));
                shelvesgroupEntity.setShelvescount(shelvesgroupEntity.getShelvescount()+1);
                shelvesgroupService.updateById(shelvesgroupEntity);
            }

        }

        goodshelvesService.save(goodshelves);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:goodshelves:update")
    public R update(@RequestBody GoodshelvesEntity goodshelves){
        ValidatorUtils.validateEntity(goodshelves);
        goodshelvesService.updateById(goodshelves);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("sys:goodshelves:delete")
    public R delete(@PathVariable("id") Integer id){
        goodshelvesService.removeById(id);

        return R.ok();
    }

    /**
     * 查询所有货架信息
     */
    @RequestMapping("/alllist")
    @RequiresPermissions("sys:goodshelves:alllist")
    public R alllist(){
        List<GoodshelvesEntity> alllist = goodshelvesService.list(new QueryWrapper<GoodshelvesEntity>());
        return R.ok().put("alllist", alllist);
    }


    /**
     * 统计信息
     */
    @RequestMapping("/infoStatistics")
    @RequiresPermissions("sys:goodshelves:infoStatistics")
    public R infoStatistics(@RequestParam Map<String, Object> params){
        ShelvesAnalysisStatisticsEntity goodshelves = goodshelvesService.getStatistics(params);

        return R.ok().put("goodshelves", goodshelves);
    }

    /**
     * 获取所有的渠道
     */
    @RequestMapping("/infoFranchisee")
    @RequiresPermissions("sys:goodshelves:infoFranchisee")
    public R infoFranchisee(){
        List<SysUserEntity> franchisee = goodshelvesService.getFranchisee();

        return R.ok().put("franchisee", franchisee);
    }
}
